package com.beordie.control;

import com.beordie.model.Grade;
import com.beordie.model.Student;
import com.beordie.service.IGradeService;
import com.beordie.service.IStudentService;
import com.beordie.service.impl.GradeServiceImpl;
import com.beordie.service.impl.StudentServiceImpl;
import com.beordie.utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname StudentController
 * @Description 学生相关请求的处理类
 * @Date 2021/6/8 22:01
 * @Created 30500
 */
@WebServlet(urlPatterns = "/Educational/student/student")
public class StudentController extends HttpServlet {
    private IStudentService service = new StudentServiceImpl();
    private IGradeService gradeService = new GradeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp){
        String methodName = req.getParameter("method");
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            try {
                method.invoke(this, req, resp);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 获取学生信息
     * @author 30500
     * @date 2021/6/9 16:51
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 拿到前端返回模糊查询的条件信息
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");
        String stuSex = req.getParameter("stuSex");
        stuSex = stuSex == null || "".equals(stuSex) ? "-1": stuSex;
        // 获取分页的页码
        String pageIndex = req.getParameter("pageIndex");
        PageUtils pageUtils = new PageUtils();
        pageUtils.setPageIndex(pageIndex == null ? 0 : Integer.valueOf(pageIndex));
        pageUtils.setPageSize(6);

        // 分页查询到的数据信息
        List<Student> students = service.getAllStudents(stuName, stuNo, stuSex, pageUtils);
        // 按照条件查询到的所有的结果条数
        int total = service.getCOunt(stuName, stuNo, stuSex);
        pageUtils.setDataList(students);
        pageUtils.setTotal(total);

        // 保存模糊查询的条件
        req.setAttribute("stuName",stuName);
        req.setAttribute("stuNo",stuNo);
        req.setAttribute("stuSex",stuSex);

        req.setAttribute("pageUtils", pageUtils);
        req.getRequestDispatcher("list.jsp").forward(req, resp);
    }

    /**
     * @description 更新学生信息
     * @author 30500
     * @date 2021/6/9 16:51
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void updateInit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String stuNo = req.getParameter("stuId");
        Student student = service.getByStuId(stuNo);
        List<Grade> list = gradeService.getAll();

        if (student != null) {
            req.setAttribute("stu", student);
            req.setAttribute("grades", list);
            req.getRequestDispatcher("edit.jsp").forward(req, resp);
        } else {
            resp.sendRedirect("/Educational/student/list.jsp");
        }
    }

    /**
     * @description 删除学生信息
     * @author 30500
     * @date 2021/6/9 22:55
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("stuId");
        int result = service.delByStuId(stuId);
        if (result > 0) {
            resp.getWriter().print("<script>alert('删除成功');location.href='/Educational/student/student?method=list'</script>");
        } else {
            resp.getWriter().print("<script>alert('删除失败');location.href='/Educational/student/student?method=list'</script>");
        }
    }

    /**
     * @description 新增学生信息请求方法
     * @author 30500
     * @date 2021/6/10 12:16
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取前端返回的所有信息数据
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");
        int stuSex = Integer.parseInt(req.getParameter("stuSex"));
        String stuPhone = req.getParameter("stuPhone");
        String stuEmail = req.getParameter("stuEmail");
        String stuRegistered = req.getParameter("stuRegistered");
        String stuAddress = req.getParameter("stuAddress");
        String stuProfession = req.getParameter("stuProfession");
        String stuPolitics = req.getParameter("stuPolitics");
        String stuIdNumber = req.getParameter("stuIdNumber");
        Date stuRegdate = new Date();
        int stuState = 1;
        String stuIntroduction = req.getParameter("stuIntroduction");
        int stuGid = Integer.parseInt(req.getParameter("stuGrade"));
        System.out.println(stuName);
        // 创建并填充到 student 的对象中
        Student student = new Student();
        student.setStuName(stuName);
        student.setStuNo(stuNo);
        student.setSex(stuSex);
        student.setPhone(stuPhone);
        student.setEmail(stuEmail);
        student.setRegistered(stuRegistered);
        student.setAddress(stuAddress);
        student.setProfession(stuProfession);
        student.setPolitics(stuPolitics);
        student.setIdNumber(stuIdNumber);
        student.setRedDate(stuRegdate);
        student.setState(stuState);
        student.setIntroduction(stuIntroduction);
        student.setgId(stuGid);

        // 向数据库保存数据
        int result = service.addAllInfo(student);
        if (result > 0) {
            resp.getWriter().print("<script>alert('添加成功');location.href='/Educational/student/student?method=list'</script>");
        } else {
            req.getRequestDispatcher("add.jsp").forward(req, resp);
        }
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        // 获取前端返回的所有信息数据
        int stuId = Integer.parseInt(req.getParameter("stuId"));
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");
        int stuSex = Integer.parseInt(req.getParameter("stuSex"));
        String stuPhone = req.getParameter("stuPhone");
        String stuEmail = req.getParameter("stuEmail");
        String stuRegistered = req.getParameter("stuRegistered");
        String stuAddress = req.getParameter("stuAddress");
        String stuProfession = req.getParameter("stuProfession");
        String stuPolitics = req.getParameter("stuPolitics");
        String stuIdNumber = req.getParameter("stuIdNumber");
        Date stuRegdate = new Date();
        int stuState = 1;
        String stuIntroduction = req.getParameter("stuIntroduction");
        int stuGid = Integer.parseInt(req.getParameter("stuGrade"));

        // 创建并填充到 student 的对象中
        Student student = new Student();
        student.setStuId(stuId);
        student.setStuName(stuName);
        student.setStuNo(stuNo);
        student.setSex(stuSex);
        student.setPhone(stuPhone);
        student.setEmail(stuEmail);
        student.setRegistered(stuRegistered);
        student.setAddress(stuAddress);
        student.setProfession(stuProfession);
        student.setPolitics(stuPolitics);
        student.setIdNumber(stuIdNumber);
        student.setRedDate(stuRegdate);
        student.setState(stuState);
        student.setIntroduction(stuIntroduction);
        student.setgId(stuGid);

        int result = service.updateInfo(student);
        if (result > 0) {
            resp.getWriter().print("<script>alert('更新成功');location.href='/Educational/student/student?method=list'</script>");
        } else {
            resp.getWriter().print("<script>alert('更新失败');location.href='/Educational/student/student?method=list'</script>");
        }
    }
}
